<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  class Email_tmp extends CI_Controller{
      protected $_templates;
      function __construct() {
		parent::__construct();
		$this->pre_message = "";
		$this->session->set_userdata(array('Url'=>uri_string()));
		$this->load->model('email_tmp_model','email_tmp');
		$this->lang->load('email_tmp'); 
		$this->load->library('string_library');
      }   
	  function index(){
		  $this->acl->check('view','mod_access','email_tmp',base_url());
		  $this->listemail_tmp();
	  }
      function listemail_tmp(){
		  	$this->acl->check('view','mod_access','email_tmp',base_url());
            $data['title'] = 'Danh sách Email templates' ;
            $data['add'] = 'email_tmp/add';
            $config['base_url'] = base_url().'email_tmp/listemail_tmp/'.(int)$this->uri->segment(3);  
            $data['num'] = $this->email_tmp->get_num_data();
           	$config['total_rows']   =  $data['num'];
         	$config['per_page']  =   '20';
           	$config['uri_segment'] = 4;   
            $this->pagination->initialize($config);
            $data['list'] =   $this->email_tmp->get_all_data($config['per_page'],(int)$this->uri->segment('4'));
            $data['pagination']    = $this->pagination->create_links();       
            $this->_templates['page'] = 'email_tmp/listemail_tmp';
            $this->site_lib->load($this->_templates['page'],$data); 
      }
	   function add(){
		   	$this->acl->check('add','mod_access','email_tmp',base_url());                    
			$data['title'] = 'Thêm mới email template';
			$this->form_validation->set_rules('code',lang('email.code'),'trim|required');
			$this->form_validation->set_rules('name',lang('email.name'),'trim|required');
			$this->form_validation->set_rules('subject',lang('email.subject'),'trim|required');
			$this->form_validation->set_rules('subject_en',lang('email.subject_en'),'trim|required');
			$this->form_validation->set_rules('content',lang('email.content'),'trim|required');
			// ghi lai file cau hinh duong dan cua tinymce
		
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{
				 
				 $data = $this->build_data($_POST,1);
				 if($this->email_tmp->saveData(0,$data)){
					 $this->session->set_flashdata('message','Lưu thành công');
					 redirect('email_tmp/listemail_tmp');
				 }else{
					 $this->pre_message = 'Lưu không thành công';
				 }
					 
             }
          $data['message'] = $this->pre_message;
          $this->_templates['page'] = 'email_tmp/add';
          $this->site_lib->load($this->_templates['page'],$data);
      }
       function edit($id){ 
	   		$this->acl->check('edit','mod_access','email_tmp',base_url());                        
            $data['title'] = 'Sửa email template';
            $data['query'] = $this->email_tmp->get_object($id);
			$this->form_validation->set_rules('name',lang('email.name'),'trim|required');
			$this->form_validation->set_rules('subject',lang('email.subject'),'trim|required');
			$this->form_validation->set_rules('subject_en',lang('email.subject_en'),'trim|required');
			$this->form_validation->set_rules('content',lang('email.content'),'trim|required');
			
             if($this->form_validation->run() == FALSE){
                $this->pre_message = validation_errors();
             }else{
				 
				$data = $this->build_data($_POST,1);
				 if($this->email_tmp->saveData($id,$data)){
					 $this->session->set_flashdata('message','Lưu thành công');
					 redirect('email_tmp/listemail_tmp');
				 }else{
					 $this->pre_message = 'Lưu không thành công';
				}
		 }
          $data['message'] = $this->pre_message;
          $this->_templates['page'] = 'email_tmp/edit';
          $this->site_lib->load($this->_templates['page'],$data);
      }
      function del(){
		$this->acl->check('del','mod_access','email_tmp',base_url());
        $id = $this->uri->segment(3);
        $page = $this->uri->segment(4);

        if($this->email_tmp->delete($id))
            $this->session->set_flashdata('message','Đã xóa thành công');
        else $this->session->set_flashdata('message','Xóa không thành công');
        
        redirect('email_tmp/listemail_tmp/'.$page);
    }
    function dels(){
		$this->acl->check('dels','mod_access','email_tmp',base_url());
        if(!empty($_POST['ar_id']))
        {
            $page = (int)$this->input->post('page');
            $ar_id = $this->input->post('ar_id');

            if(!empty($_POST['btn_submit']))
            {
                for($i = 0; $i < sizeof($ar_id); $i ++) {
                    if ($ar_id[$i]){
                        if($this->email_tmp->delete($ar_id[$i]))
                            $this->session->set_flashdata('message','Đã xóa thành công');
                        else $this->session->set_flashdata('message','Xóa không thành công');
                    }
                }
            }
        }
        redirect('email_tmp/listemail_tmp/'.$page);
    }
   
    function build_data($data,$flag=1)
	{//flag=1:0?insert:update
			$dataUpdate["code"]=trim($data["code"]);
			$dataUpdate["name"]=trim($data["name"]);
			$dataUpdate["subject"]=trim($data["subject"]);
			$dataUpdate["subject_en"]=trim($data["subject_en"]);
			$dataUpdate["param"]=trim($data["param"]);
			$dataUpdate["content"]=trim($data["content"]);
			$dataUpdate["content_en"]=trim($data["content_en"]);
			if(isset($data["bl_active"])){
			$dataUpdate["bl_active"]=trim($data["bl_active"]);
		}else{
			$dataUpdate["bl_active"]=0;
		}
		return $dataUpdate;
	}

  }
?>
